package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.StudentDao;
import com.itheima.edu.info.manager.domain.Student;

public class StudentService {
    private StudentDao studentDao = new StudentDao();

    //判断学号是否已存在
    public boolean isExists(String id) {
        Student[] students = studentDao.findAllStudent();
        //标记位思想
        boolean exists = false;
        //遍历判断
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && students[i].getId().equals(id)) {
                exists = true;
                break;
            }
        }
        return exists;
    }

    //添加学生
    public boolean addStudent(Student student) {
        return studentDao.addStuendt(student);
    }

    //删除学生
    public void deleteStudentById(String delId) {
        studentDao.deleteStundetById(delId);
    }

    //查找所有学生
    public Student[] findALLStudent() {
        Student[] students = studentDao.findAllStudent();
        //判断数组是否为空
        boolean empty = true;
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null) {
                empty = false;
                break;
            }
        }
        //如果是空则返回空值,如果非空则返回数组
        if(empty){
            return null;
        }else{
            return students;
        }
    }
    //修改学生方法
    public void updateStudent(Student student) {
        studentDao.updateStudent(student);
    }
}
